const fs = require('fs')
const {join} = require('path')

exports.index = async ctx => {
  if(ctx.session.isNew) {
    // 没有登录
    ctx.status = 404
    return await ctx.render("404", {title: "404"})
  }

  const id = ctx.params.id

  const arr = fs.readdirSync(join(__dirname, "../views/admin"))

  let flag = false

  // arr ['admin-article.pug', 'admin-user.pug', 'admin-userface.pug', 'admin.pug']

  // 读取所有的admin 下面的所有模块  便于 扩展  当新增功能是无需再添加逻辑
  arr.forEach(v => {
    const name = v.replace(/^(admin\-)|(\.pug)$/g, "")

    // article comment user userface admin
    
    if(name === id) {
      flag = true
    }
  })
  
  if(flag) {
    await ctx.render("./admin/admin-" + id, {
      role: ctx.session.role
    })

  } else {
    ctx.status = 404
    await ctx.render("404", {title: '404'})
  }

}